/*
 * Copyright (C) 2009 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metalframework.jsf.scope;

import java.util.*;

import org.metalframework.jsf.scope.impl.*;


public abstract class FacesScope {

	private static final ApplicationScope applicationScope = new ApplicationScope();
	private static final SessionScope sessionScope = new SessionScope();
	private static final ConversationScope conversationScope = new ConversationScope();
	private static final PageScope pageScope = new PageScope();
	private static final RequestScope requestScope = new RequestScope();
	private static final FlashScope flashScope = new FlashScope();

	public static ApplicationScope getApplicationScope() {
		return applicationScope;
	}
	
	public static SessionScope getSessionScope() {
		return sessionScope;
	}
	
	public static ConversationScope getConversationScope() {
		return conversationScope;
	}

	public static PageScope getPageScope() {
		return pageScope;
	}
	
	public static RequestScope getRequestScope() {
		return requestScope;
	}
	
	public static FlashScope getFlashScope() {
		return flashScope;
	}
	
	public static FacesScope getScopeByType(FacesScopeType type) {
		switch (type) {
			case APPLICATION:
				return getApplicationScope();
			case SESSION:
				return getSessionScope();
			case CONVERSATION:
				return getConversationScope();
			case PAGE:
				return getPageScope();
			case REQUEST:
				return getRequestScope();
			case FLASH:
				return getFlashScope();
			default:
				throw new IllegalArgumentException("Unknown scope type: " + type);
		}
	}
	
	public static List<FacesScope> getAllScopes() {
		List<FacesScope> scopes = new ArrayList<FacesScope>();
		
		for (FacesScopeType type : FacesScopeType.getTypesOrderedBySize()) {
			scopes.add(getScopeByType(type));
		}
		
		return scopes;
	}
	
	public abstract <T> T get(String name);
	
	public abstract void set(String name, Object obj);

	public abstract void remove(String name);

	public abstract boolean contains(String name);
	
	public abstract void onStartRequest();
	
	public abstract void onEndRequest();

}
